package main;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class SpriteStore {
	
	private static SpriteStore single = new SpriteStore();
	private static HashMap<String, Sprite> sprites = new HashMap<String, Sprite>();
	
	public static SpriteStore get() {
		return single;
	}
	
	public void loadImage( String ref ) throws IOException {
		URL url = this.getClass().getClassLoader().getResource( ref );
//		System.out.println( ref );
		BufferedImage sourceImage = ImageIO.read(url);
		
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		Image image = gc.createCompatibleImage( sourceImage.getWidth(), sourceImage.getHeight(), Transparency.BITMASK);
		image.getGraphics().drawImage(sourceImage, 0, 0, null );
		
		Sprite sprite = new Sprite( image );
		sprites.put( ref, sprite );
		
	}
	
	public Sprite getSprite( String ref ) {
		if ( sprites.get( ref ) != null ) {
			return (Sprite )sprites.get( ref );
		} else {
			return null;
		}
	}

}
